package com.gsm.service.impl;

import com.gsm.dao.SysRoleDao;
import com.gsm.entity.*;
import com.gsm.dao.SysUserDao;
import com.gsm.service.SysUserService;
import com.gsm.utils.Base64Utils;
import com.gsm.utils.IdUtils;
import com.gsm.utils.JwtUtils;
import com.gsm.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 后台管理人员账户(SysUser)表服务实现类
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private SysRoleDao sysRoleDao;


    @Override
    public Result insertSysUser(SysUser sysUser) {
        SysUser user = sysUserDao.selectSysUserMaxShowOrder(sysUser.getIphoneNo());
        int showOrder = 0;
        if (user != null) {
            showOrder = user.getShowOrder() + 1;
        }
        long userId = idUtils.nextId();
        SysUser tmpUser = SysUser.builder()
                .userId(userId)
                .userName(sysUser.getUserName())
                .iphoneNo(sysUser.getIphoneNo())
                .po(Base64Utils.base64Encode(sysUser.getPo()))
                .pc(MD5Utils.getMD5(sysUser.getPc()))
                .status(1)
                .showOrder(showOrder)
                .createTime(new Date())
                .build();
        sysUserDao.insertSysUser(tmpUser);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 根据 主键 userId 删除
     *
     * @param userId 主键
     * @return
     */
    @Override
    public Result deleteSysUserByUserId(Long userId) {
        sysUserDao.deleteSysUserByUserId(userId);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 修改
     *
     * @param sysUser 实例对象
     * @return
     */
    @Override
    public Result updateSysUserByUserId(SysUser sysUser) {
        sysUserDao.updateSysUserByUserId(sysUser);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param userId 主键
     * @return
     */
    @Override
    public Result selectSysUserByUserId(Long userId) {
        SysUser sysUser = sysUserDao.selectSysUserByUserId(userId);
        Result result = new Result(true, StatusCode.OK, "", sysUser);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param sysUser 实例对象
     * @return
     */
    @Override
    public Result selectSysUserList(SysUser sysUser) {
        List<SysUser> list = sysUserDao.selectSysUserList(sysUser);
        Result result = new Result(true, StatusCode.OK, "", list);
        return result;
    }

    @Override
    public Result selectSysUserByAccountAndPwd(SysUser sysUser) {
        //根据手机号查询用户信息
        SysUser user = sysUserDao.selectSysUserByIphoneNo(sysUser.getIphoneNo());
        if (user == null) {
            Result result = new Result(false, StatusCode.ERROR, "", "用户不存在");
            return result;
        }
        //用户密码明文
        String po = user.getPo();
        String po1 = Base64Utils.base64Encode(sysUser.getPo());
        if (!po1.equals(po)) {
            Result result = new Result(false, StatusCode.ERROR, "", "用户名或密码错误");
            return result;
        }
        //生成Token
        SysRole sysRole = sysRoleDao.selectSysRoleByUserId(user.getUserId());
        String userId = Long.toString(user.getUserId());
        String userName = user.getUserName();
        String roleId = Long.toString(sysRole.getRoleId());
        String roleName = sysRole.getRoleName();
        String token = jwtUtils.getJwt(userId, userName, roleId, roleName);
        LoginUser loginUser = new LoginUser();
        loginUser.setToken(token);
        user.setPo(null);
        user.setPc(null);
        loginUser.setSysUser(user);
        Result result = new Result(true, StatusCode.OK, "", loginUser);
        return result;
    }
}